# ---------- ---------- ---------- ---------- ---------- ----------
#
# fenrisg Makefile
#
#    1) config, vars, and target
#    2) functions
#    3) phonies
#
# ---------- ---------- ---------- ---------- ---------- ----------


BUILD=bin
BIN=$(BUILD)/fenrisg
MAN=$(BUILD)/fenrisg.1
BIN_DIR=$(if $(i),$(i),$$HOME/.local/bin)
MAN_DIR=$(if $(m),$(m),$$HOME/.local/share/man/man1)


target: help


# ---------- ---------- ---------- ---------- ---------- ----------
# functions
# ---------- ---------- ---------- ---------- ---------- ----------

define append_to_build
	@cat $(1) >> $(BIN)
	@echo "\n" >> $(BIN)
endef

define lint_bin
	@shellcheck bin/fenrisg || echo ""
endef

define lint_src
	@shellcheck -s bash \
            src/*.sh \
            src/rss/*.sh \
            src/site/*.sh \
            src/sitemap/*.sh \
            src/utils/*.sh \
            || \
            echo ""
	@#shellcheck -s bash src/sitemap/*.sh
endef

define lint_tests
	@shellcheck -s bash \
            tests/*.sh \
            tests/mocks/coreutils/* \
            tests/mocks/fenrisg/*.sh \
            || \
            echo ""
	@shellcheck -s bash \
            tests/test_* -e SC1090 \
            || \
            echo ""
endef

define run_tests
	@cd ./tests && \
            ./test_file_extentions && \
            ./test_get_directory_depth_offset && \
            ./test_get_absolute_path && \
            ./test_get_output_file && \
            ./test_main
endef


# ---------- ---------- ---------- ---------- ---------- ----------
# phonies
# ---------- ---------- ---------- ---------- ---------- ----------

.PHONY: help
help:
	@echo "Usage: make [PHONY] [OPTIONS]"
	@echo "PHONY:"
	@sed -n -e "/sed/! s/\.PHONY: /    /p" Makefile
	@echo "OPTIONS:"
	@echo "    i=[installation directory for binaries] - Defaults to \$$HOME/.local/bin"
	@echo "    m=[installation directory for manuals] - Defaults to \$$HOME/.local/share/man/man1"


.PHONY: build
build: clean build-bin build-manual


.PHONY: build-bin
build-bin:
	@mkdir -p $(BUILD) && touch $(BIN) && chmod +x $(BIN)
	@$(call append_to_build,src/shebang.sh)
	@$(call append_to_build,src/usage.sh)
	@$(call append_to_build,src/version.sh)
	@$(call append_to_build,src/utils/functions.sh)
	@$(call append_to_build,src/inputs.sh)
	@$(call append_to_build,src/rss/build_rss.sh)
	@$(call append_to_build,src/site/build_site.sh)
	@$(call append_to_build,src/site/process_file.sh)
	@$(call append_to_build,src/sitemap/build_sitemap.sh)
	@$(call append_to_build,src/main_function.sh)
	@$(call append_to_build,src/main_script_call.sh)


.PHONY: build-manual
build-manual:
	@mkdir -p $(BUILD) && touch $(MAN)
	@pandoc -s -f markdown-smart -t man fenrisg.1.md -o $(MAN) || exit


.PHONY: clean
clean:
	@rm -rf $(BUILD)


.PHONY: install
install: build
	@mkdir -p $(BIN_DIR) && \
             mkdir -p $(MAN_DIR)
	@echo "Installing into "$(BIN_DIR)
	@install -Dm755 $(BIN) $(BIN_DIR)
	@echo "Manual into "$(MAN_DIR)
	@install -Dm644 $(MAN) $(MAN_DIR)


.PHONY: lint
lint: lint-src lint-tests lint-bin


.PHONY: lint-bin
lint-bin: build
	@$(call lint_bin)


.PHONY: lint-src
lint-src:
	@$(call lint_src)


.PHONY: lint-tests
lint-tests:
	@$(call lint_tests)


.PHONY: tests
tests: build
	@$(call run_tests)
